/* 
 * @Author: Dorange.Wang
 * @Date: 2021-04-01 15:27:13
 * @LastEditors: wangming
 * @LastEditTime: 2022-01-06 16:03:13
 * @Description: file content
 */
import qs from "qs";
import stores from 'store2'
import { ACCESS_TOKEN } from '@/utils/const'

export const appUnique = (location.pathname.match(
  /\/(designer|preview|mobile|browser)(\/\d+\.\d+\.\d+)?(\/co:[0-9a-z]+)?\/app:([\w\.]{7,})\/?/
) || [])[4]

export class Accesstoken {
  static getTokenName() {
    if (process.env.VUE_APP_USER === 'APP' && appUnique) {
      return ACCESS_TOKEN + appUnique
    } else {
      return ACCESS_TOKEN
    }
  }
  static getAccessToken() {
    this.tokenName = this.getTokenName()
    return stores.get(this.tokenName)
  }
  static setAccessToken(tokenName) {
    this.tokenName = this.getTokenName()
    stores.set(this.tokenName, tokenName)
  }
}





// /**
//  * 抓取url上的参数值缓存
//  * @param {*} variable
//  * @returns
//  */
const cacheData = {
  [ACCESS_TOKEN]: 'token'
}
var query = window.location.search.substring(1);
var queryData = qs.parse(query)
for (let i in cacheData) {
  let key = cacheData[i]
  let value = queryData[key]
  if (value) {
    stores.set(i, value)
    queryData[key] = undefined
  }
}
let { hash, pathname, origin, href } = window.location
let search = qs.stringify(queryData)
let url = origin + pathname + (search ? "?" + search : '') + hash
if (url !== href) {
  window.history.replaceState({}, "", url)
}


// const getQueryParameters = () => {
//   const url = window.location.href
//   const search = url.split('?')[1]
//   if (!search) {
//     return {}
//   }
//   return JSON.parse(
//     '{"' +
//       decodeURIComponent(search)
//         .replace(/"/g, '\\"')
//         .replace(/&/g, '","')
//         .replace(/=/g, '":"') +
//       '"}'
//   )
// }

// const params = getQueryParameters()
// const data = {}
// // 单点登录
// const token = (params.token || '').replace('#/', '')
// const lang = (params.lang || '').replace('#/', '')
// if (token) {
//   data[ACCESS_TOKEN] = token
//   data[TOKEN] = token
// }
// Object.keys(data).length > 0 && stores(data)
// const url = window.location.href
// let redirect = window.location.href
// if (token) {
//   const regToken = RegExp('[\\?&]?token=' + token, 'g')
//   redirect = redirect.replace(regToken, '')
// }
// if (lang) {
//   redirect = redirect.replace(`&lang=${lang}`, '')
//   redirect = redirect.replace(`lang=${lang}`, '')
// }
// url !== redirect && window.location.replace(redirect)
